<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Recos配置详情</title>
        <meta name="description" content="Recos">
        <meta name="keywords" content="Recos">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/sidebar.css">
        <link href="../plugins/quill/quill.snow.css" rel="stylesheet">
        <link href="../plugins/quill/quill.bubble.css" rel="stylesheet">
        <link href="../plugins/quill/quill.core.css" rel="stylesheet">
        <script src="../plugins/quill/quill.js"></script>
        <style>
            #textarea {
                background-color: #687084;
                color: rgba(255, 255, 255, 0.99);
            }
        </style>
    </head>
    <body class="hold-transition">

        <div id="app" class="app">
<!--            <iframe src="sidebar.html"></iframe>-->
            <!-- 使用JavaScript加载侧边栏内容 -->
            <div id="sidebar-container"></div>

            <!-- 其他内容 -->
            <script>
                // 使用JavaScript加载侧边栏内容
                const sidebarContainer = document.getElementById("sidebar-container");
                const xhr = new XMLHttpRequest();
                xhr.open("GET", "sidebar.html", true);
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === 4 && xhr.status === 200) {
                        sidebarContainer.innerHTML = xhr.responseText;
                    }
                };
                xhr.send();
            </script>
            <div id="page-container">

                <div class="content-header">
                    <h1 aria-label="center">编辑配置</h1>
                    <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                        <el-breadcrumb-item :to="{ path: '/' }"></el-breadcrumb-item>
                    </el-breadcrumb>
                </div>

                <div class="app-container">
                    <div class="box">
                        *Data ID<el-input id="data-id-input" v-model="conf.dataId" :disabled="edit"></el-input><br>
                        *GROUP ID<el-input id="group-id-input" v-model="conf.groupId" :disabled="edit"></el-input><br>
                        归属应用<el-input id="group-id-input" v-model="conf.appName" :disabled="edit"></el-input><br>
                        描述信息<el-input id="group-id-input" v-model="conf.cDesc" :disabled="edit"></el-input><br>
                        配置格式<template >
                                <el-radio :disabled="edit" v-model="filetype" label="1">properties</el-radio>
                                <el-radio :disabled="edit" v-model="filetype" label="2">yml</el-radio>
                            </template>
                        <br>
                        <br>
                        配置内容<el-input
                                id="textarea"
                                type="textarea"
                                :rows="30"
                                placeholder="请输入内容"
                                v-model="conf.content">
                        </el-input>
                        <el-button slot="reference" v-show="his" type="primary" size="mini" prop="href"  @click="uploadOne">发布</el-button>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script src="../plugins/elementui/index.js"></script>


    <script src="../plugins/quill/vue-quill-editor.js"></script>
    <script src="../js/token.js"></script>
    <script>

        var vue = new Vue({
            el: '#app',
            data:{
                edit:false,
                his:true,
                textarea:'',
                filetype:'1',
                conf:{
                    id:'',
                    dataId:'',
                    groupId:'DEFAULT_GROUP',
                    cDesc:'',
                    appName:'',
                    content: '',
                },
                recosServerPath: "/recos/configinfo"
            },
            //钩子函数，VUE对象初始化完成后自动执行
            created() {
                if (!getToken() || getToken()==''){
                    window.location.href="./login.html"

                }
                this.initData();
            },
            methods: {
                initData(){
                    function getQueryString(name) {
                        let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
                        let r = window.location.search.substr(1).match(reg);
                        if (r != null) {
                            return decodeURIComponent(r[2]);
                        };
                        return null;
                    }
                    var id=getQueryString("id")
                    var his=getQueryString("his")
                    if (id){
                        if (!his){
                            this.initDataForConfig(id)
                        }else {
                            this.initDataForHisConfig(id)
                        }
                    }

                },
                initDataForConfig(id){
                    this.conf.id=id
                    this.edit=true
                    //todo （编辑页面） 这里需要把id传回去，回显数据
                    axios.get(this.recosServerPath+"/getOne/"+id,getAxiosConfig()).then((res)=>{
                        if (res.data.success){
                            this.conf.appName = res.data.data.appName;
                            this.conf.cDesc = res.data.data.cDesc;
                            this.conf.content = res.data.data.content;
                            this.conf.groupId = res.data.data.groupId;
                            this.conf.id = res.data.data.id;
                            this.conf.dataId = res.data.data.dataId;
                        }else if (res.data.code==-555){
                            window.location.href="./login.html";
                        }
                    }).catch((error)=>{
                        if (error == "Error: Request failed with status code 403"){
                            this.$message.error("无操作权限");
                        }
                    });
                },
                initDataForHisConfig(id){
                    this.conf.id=id
                    this.edit=true
                    this.his=false
                    //his页面,不允许发布编辑
                    axios.get("/recos/hisconfiginfo/getOne/"+id,getAxiosConfig()).then((res)=>{
                        if (res.data.success){
                            this.conf.appName = res.data.data.appName;
                            this.conf.cDesc = res.data.data.cDesc;
                            this.conf.content = res.data.data.content;
                            this.conf.groupId = res.data.data.groupId;
                            this.conf.id = res.data.data.id;
                            this.conf.dataId = res.data.data.dataId;
                        }else if (res.data.code==-555){
                            window.location.href="./login.html";
                        }
                    }).catch((error)=>{
                        if (error == "Error: Request failed with status code 403"){
                            this.$message.error("无操作权限");
                        }
                    });
                },
                uploadOne(){
                    var route="";
                    if (this.edit){
                        //走update
                        route="/update";

                    }else {
                        route="/add";
                        this.conf.dataId += (this.filetype==1)?".properties":".yml";
                    }
                    this.$confirm('确定发布吗?, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        axios.post(this.recosServerPath+route,this.conf,getAxiosConfig()).then((res)=>{
                            if (res.data.success){
                                alert("发布成功!")
                            }else if (res.data.code==-555){
                                window.location.href="./login.html";
                            }
                        }).catch((error)=>{
                            if (error == "Error: Request failed with status code 403"){
                                this.$message.error("无操作权限");
                            }
                        });
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消发布'
                        });
                    });



                }

            }
        });
        Vue.use(window.VueQuillEditor);
    </script>
<style>
    .item {
        margin-top: 10px;
        margin-right: 40px;
    }
</style>
</html>